﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class ProductType : BasePage
    {
        private IProductTypeBLL productTypeBLL = IocContainer.Instance.Resolve<IProductTypeBLL>();
        private IBrandBLL brandBLL = IocContainer.Instance.Resolve<IBrandBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ControlAuthority(new List<WebControl> { btnhidDelete, btnUpdate, btnDelete, btnAddSubNode, btnAddSameLevel }, 21);
                this.ddlBrand.DataSource = brandBLL.GetBrand("", null);
                this.ddlBrand.DataBind();

                this.litTreeView.Text= BuildTreeView();
            }
        }
        private string BuildTreeView()
        {
            int brandId = 0;
            int.TryParse(this.ddlBrand.SelectedValue, out brandId);

            DataTable dtDepartment = productTypeBLL.GetAllProductTypeByBrand(brandId);

            return GenerateTreeHtmlFragment(dtDepartment, 0);
        }

        private string GenerateTreeHtmlFragment(DataTable dtDepartment, int parentId)
        {
            StringBuilder result = new StringBuilder("");
            if (dtDepartment != null && dtDepartment.Rows.Count > 0)
            {
                DataRow[] drRoots = dtDepartment.Select("ParentId = " + parentId);
                foreach (DataRow row in drRoots)
                {
                    result.AppendFormat("<ul><li><a href=\"javascript:void(0)\" title=\"{0}\" tname=\"{1}\" tvalue=\"{2}\" tlevel=\"{4}\" tcode=\"{5}\">{3}</a>", row["Description"], row["ParentId"], row["Id"], row["Name"], row["Level"], row["Code"]);

                    int typeId = 0;
                    int.TryParse(row["Id"].ToString(), out typeId);
                    result.Append(GenerateTreeHtmlFragment(dtDepartment, typeId));
                    result.Append("</li></ul>");
                }
            }
            return result.ToString();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int typeId = 0;
                int.TryParse(this.txtHidTypeId.Value.Trim(), out typeId);
                if (typeId >= 0)
                {
                    productTypeBLL.Delete(typeId);

                    this.litTreeView.Text = BuildTreeView();

                   // Alert("删除成功！");
                }
                else
                {
                    Alert("请选择要删除的产品分类！");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int brandId = 0;
                int.TryParse(this.ddlBrand.SelectedValue, out brandId);

                Skyworth.CRM.Service.Entity.ProductType productType = new Skyworth.CRM.Service.Entity.ProductType();
                productType.BrandId = brandId;
                productType.Description = this.txtDescription.Text;
                productType.ParentId = Convert.ToInt16(this.txtHidParentId.Value);
                productType.Level = Convert.ToInt16(this.txtHidLevel.Value);
                productType.Id = Convert.ToInt16(this.txtHidTypeId.Value);
                productType.Code = this.txtTypeCode.Text;
                productType.Name = this.txtTypeName.Text;

                productTypeBLL.Update(productType);

                litTreeView.Text = BuildTreeView();

                //Alert("修改成功！");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }

        protected void btnAddSameLevel_Click(object sender, EventArgs e)
        {
            try
            {
                int brandId = 0;
                int.TryParse(this.ddlBrand.SelectedValue, out brandId);

                Skyworth.CRM.Service.Entity.ProductType productType = new Skyworth.CRM.Service.Entity.ProductType();
                productType.BrandId = brandId;
                productType.Name = this.txtTypeName.Text;
                productType.Code = this.txtTypeCode.Text;
                productType.Description = this.txtDescription.Text;
                productType.ParentId = Convert.ToInt16(this.txtHidParentId.Value);
                productType.Level = Convert.ToInt16(this.txtHidLevel.Value);

                productTypeBLL.Add(productType);

                litTreeView.Text = BuildTreeView();

                //Alert("添加成功！");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }

        protected void btnAddSubNode_Click(object sender, EventArgs e)
        {
            try
            {
                int brandId = 0;
                int.TryParse(this.ddlBrand.SelectedValue, out brandId);

                Skyworth.CRM.Service.Entity.ProductType productType = new Skyworth.CRM.Service.Entity.ProductType();
                productType.BrandId = brandId;
                productType.Name = this.txtTypeName.Text;
                productType.Code = this.txtTypeCode.Text;
                productType.Description = this.txtDescription.Text;
                productType.ParentId = Convert.ToInt16(this.txtHidTypeId.Value);
                productType.Level = Convert.ToInt16(this.txtHidLevel.Value) +1;

                productTypeBLL.Add(productType);

                litTreeView.Text = BuildTreeView();

                //Alert("添加成功！");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }

        protected void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtHidLevel.Value = string.Empty;
            this.txtHidTypeId.Value = string.Empty;
            this.txtHidParentId.Value = string.Empty;
            this.txtTypeCode.Text = string.Empty;
            this.txtTypeName.Text = string.Empty;
            this.txtTypeCode.Text = string.Empty;
            this.txtDescription.Text = string.Empty;

            litTreeView.Text = BuildTreeView();
        }
    }
}
